﻿Option Explicit On
Option Strict On

Partial Class private_secure_members_manage_Associate
    Inherits System.Web.UI.Page



    Dim u As MembershipUser

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        u = Membership.GetUser(HttpContext.Current.User.Identity.Name)

        If grdEntities.SelectedValue Is Nothing Then
            If Page.IsPostBack Then
                VisibleOrNot(True)
            Else
                VisibleOrNot(False)
            End If
        Else
            VisibleOrNot(True)
        End If

    End Sub
    Sub VisibleOrNot(ByVal Parameter As Boolean)
        Panel1.Visible = Parameter
        Panel2.Visible = Parameter
        Label1.Visible = Parameter
        Label2.Visible = Parameter
    End Sub
    Protected Sub SqlEntities_ds_Selecting(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlEntities_ds.Selecting
        'Important to add the " sign for the parameter name
        e.Command.Parameters("@UserId").Value = CType(u.ProviderUserKey, Guid)
        'SqlEntities_ds.SelectParameters("UserId").DefaultValue = CType(u.ProviderUserKey, Guid).ToString()
    End Sub

    Protected Sub SqlAssociates_ds_Selecting(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlAssociates_ds.Selecting
        'Important to add the " sign for the parameter name

        'e.Command.Parameters("@EntityId").Value = CType(u.ProviderUserKey, Guid)
        'e.Command.Parameters("@EntityId").Value = CType(u.ProviderUserKey, Guid)
        'SqlEntities_ds.SelectParameters("UserId").DefaultValue = CType(u.ProviderUserKey, Guid).ToString()

    End Sub
    Protected Sub btnUnassign_Click(sender As Object, e As System.EventArgs) Handles btnAssign.Click
        'TODO: Determine what the selected entity is in the grdEntities grid
        'TODO: Now write code to go through each row in the grdAssociates grid and determine which item has been 'checked' in the To UnAssign column
        'TODO: Now write code to go through each row in the grdAssignable grid and determine which item has been 'checked' in the Assigned column
        'TODO: exec usp_PersonAssociateEntity_Exp_ByEntityIdAssociateId --delete dbo.PersonAssociateEntity_exp where EntityId = @EntityId and AssociateId = @AssociateId
        'perassocentitynew.Delete(1)
        'For each checked item 
        '   get the entityId from the grdEntity and the AssociateId from the grid hidden column
        Dim EntityId As Integer                                                            'Tony S. 19-April-2013
        Dim AssociateId As Integer                                                         'Tony S. 19-April-2013

        Dim ch As CheckBox
        For Each row As GridViewRow In grdAssociates.Rows
            ' Do something
            ch = CType(row.FindControl("CheckBox3"), CheckBox)
            If ch.Checked Then
                Dim index As Integer = Convert.ToInt32(grdAssociates.DataKeys(row.RowIndex).Value)

                Dim perassocentitynewTA As New LogginsLogDSTableAdapters.usp_PersonAssociateEntity_ExpTableAdapter()

                Dim perassocentitynewDT As LogginsLogDS.usp_PersonAssociateEntity_ExpDataTable
                perassocentitynewDT = perassocentitynewTA.GetData(Nothing)
                Dim getid = From g In perassocentitynewDT.AsEnumerable() Where g.AssociateId = CType(index, Integer?) AndAlso g.EntityId = Convert.ToInt32(grdEntities.SelectedValue) Select g

                For Each assocRow As LogginsLogDS.usp_PersonAssociateEntity_ExpRow In getid
                    ' perassocentitynewTA.Delete(assocRow.Id)
                    EntityId = Convert.ToInt32(grdEntities.SelectedValue)                  'Tony S. 19-April-2013
                    AssociateId = index  'Tony S. 19-April-2013

                    perassocentitynewTA.Delete(EntityId, AssociateId)                      'Tony S. 19-April-2013
                Next
            End If
        Next

        grdAssignables.DataBind()
        grdAssociates.DataBind()
        'Response.Redirect("MyAssociate.aspx")

    End Sub

    Protected Sub btnAssign_Click(sender As Object, e As System.EventArgs) Handles btnUnassign.Click
        'TODO: Determine what the selected entity is in the grdEntities grid
        'TODO: Now write code to go through each row in the grdAssignables grid and determine which item has been 'checked' in the To Assign column
        'TODO: For each item checked do the following
        'TODO: insert into dbo.PersonAssociateEntity_exp (EntityId, AssociateId, Active) values (@EntityId, @AssociateId,1)
        Dim ch As CheckBox

        For Each row As GridViewRow In grdAssignables.Rows
            ' Do something
            ch = CType(row.FindControl("CheckBox1"), CheckBox)
            If ch.Checked Then
                Dim index As String = Convert.ToString(grdAssignables.DataKeys(row.RowIndex).Value)
                Dim perassocentitynewTA As New LogginsLogDSTableAdapters.usp_PersonAssociateEntity_ExpTableAdapter()
                perassocentitynewTA.Insert(CType(index, Integer?), CType(grdEntities.SelectedValue, Integer?), True)
            End If
        Next
        grdAssociates.DataBind()
        grdAssignables.DataBind()
        'Response.Redirect("MyAssociate.aspx")

    End Sub

    Protected Sub btnDeleteAssociate_Click(sender As Object, e As System.EventArgs) Handles btnDeleteAssociate.Click
        'todo: This code will delete the Associate and call this stored procedure 
        'dbo.usp_DeleteAssociate_ByPersonId @PersonId int

        'First confirm delete of the entire Associate Details

        Dim ch As CheckBox

        For Each row As GridViewRow In grdAssignables.Rows
            ' Do something
            ch = CType(row.FindControl("CheckBox1"), CheckBox)
            If ch.Checked Then
                Dim index As String = Convert.ToString(grdAssignables.DataKeys(row.RowIndex).Value)

                'EXEC dbo.usp_DeleteAssociate_ByPersonId @PersonId int

            End If
        Next
        grdAssociates.DataBind()
        grdAssignables.DataBind()
    End Sub

    Protected Sub grdEntities_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles grdEntities.SelectedIndexChanged

    End Sub
End Class
